/**
 * 
 */
package com.planner.core.service.rest.impl;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;

import com.india.RouteCode;
import com.india.StopNode;
import com.india.bus.delhi.DTCBusRouteCode;
import com.india.bus.delhi.DTCRouteFactory;
import com.india.bus.delhi.DTCRouteFactoryImpl;
import com.india.metro.delhi.DelhiMetroRouteCode;
import com.india.metro.delhi.DelhiMetroRouteFactory;
import com.india.metro.delhi.DelhiMetroRouteFactoryImpl;
import com.planner.core.dao.stub.StubUtil;
import com.planner.core.service.rest.core.RestRouteCode;
import com.planner.core.service.rest.core.RestStopNode;
import com.planner.core.vo.NodeType;

/**
 * @author pradeep
 *
 */
@Path("/static/")
public class StaticDataService {
	private static final Log LOG = LogFactory.getLog(StaticDataService.class);
	
	@GET
	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	@Path("/nodeType")
	public List<NodeType> search(@QueryParam ("searchStr") String searchStr) {
		LOG.info("Search stop performed with query string: "+searchStr);
		
		return StubUtil.getNodeTypes();
	}
	
	@GET
	@Produces({MediaType.APPLICATION_XML})
	@Path("/route/metro/all")
	public List<RestRouteCode> getAllMetroRouteCodes(){
		return getRestRouteCode(DelhiMetroRouteCode.values());
	}
	
	@GET
	@Produces({MediaType.APPLICATION_XML})
	@Path("/route/bus/all")
	public List<RestRouteCode> getAllBusRouteCodes(){
		return getRestRouteCode(DTCBusRouteCode.values());
	}
	
	
	@GET
	@Produces({MediaType.APPLICATION_XML})
	@Path("/route/metro")
	public List<RestStopNode> metroRoute(@QueryParam ("routeCode") String code) {
		LOG.info("Search stop performed with query string: "+code);
		DelhiMetroRouteFactory factory = new DelhiMetroRouteFactoryImpl();
		List<RestStopNode> nodes = getRestRoute(factory.getRoute(DelhiMetroRouteCode.valueOf(code)));
		return nodes;
	}
	
	@GET
	@Produces({MediaType.APPLICATION_XML})
	@Path("/route/bus")
	public List<RestStopNode> busRoute(@QueryParam ("routeCode") String code) {
		LOG.info("Search stop performed with query string: "+code);
		DTCRouteFactory factory = new DTCRouteFactoryImpl();
		List<RestStopNode> nodes = getRestRoute(factory.getRoute(DTCBusRouteCode.valueOf(code)));
		return nodes;
	}

	private List<RestRouteCode> getRestRouteCode(RouteCode[] values) {
		List<RestRouteCode> codes = new ArrayList<RestRouteCode>();
		for(RouteCode value : values){
			codes.add(new RestRouteCode(value));
		}
		return codes;
	}

	private List<RestStopNode> getRestRoute(List<StopNode> routeNodes) {
		List<RestStopNode> nodes = new ArrayList<RestStopNode>();
		for(StopNode stop : routeNodes){
			nodes.add(new RestStopNode(stop));
		}
		return nodes;
	}
	
}